#include "TrayIcon.h"
#include "CurrentAppHeaders.h"
#include "Multilanguage.h"

#define DONTWANT_RunNonElevated
#define NO_DLL_IMPORTS
#include "VistaTools.cxx"

BEGIN_EVENT_TABLE(TrayIcon, wxTaskBarIcon)
    EVT_MENU(wxID_EXIT,    TrayIcon::OnMenuExit)
    EVT_MENU(ID_OPENSETTINGS,    TrayIcon::OnMenuSettings)
    EVT_TASKBAR_LEFT_DOWN (TrayIcon::OnLeftClick)
    EVT_TASKBAR_RIGHT_UP(TrayIcon::OnRightClick) 
END_EVENT_TABLE()

class TrayIconMenu : public wxMenu
{
public:
    ~TrayIconMenu()
    {
        wxGetApp().m_MenuShown = NULL;
    }
};

TrayIcon::TrayIcon()
{ 
    m_insertOpenSettingsMenu = true;
    m_action = 0;
}

void TrayIcon::ShowSettingsMenu()
{
    m_insertOpenSettingsMenu = true;
}
void TrayIcon::HideSettingsMenu()
{
    m_insertOpenSettingsMenu = false;
}
void TrayIcon::OnMenuSettings(wxCommandEvent& WXUNUSED(event))
{
    if (IsVista() && IsElevated() == S_OK)
        RunElevated( NULL, Utils::GetExePath(), wxT("/s ") );
    else
        MyShellExec( NULL, NULL, Utils::GetExePath(), wxT("/s "), NULL );
}
void TrayIcon::OnMenuExit(wxCommandEvent& event)
{   
    if (event.GetInt() > 0)
    {
        m_action = event.GetInt();
        m_url = event.GetString();
    }
    else
    {
        m_action = 0;
        wxGetApp().Finish();
        //wxTheApp->Exit();
    }
}

void TrayIcon::OnLeftClick(wxTaskBarIconEvent& WXUNUSED(event))
{
    if (this->IsBalloonEvent() == true)
    {
        if (m_action == 1)
            wxLaunchDefaultBrowser(m_url);

        //if (wxGetApp().m_TNod == NULL) { }
        //else { }

        m_action = 0;
    }
}

// ---------------------------------------------------------
// Creates pop-up when the user right-click in the app icon
// ---------------------------------------------------------
void TrayIcon::OnRightClick(wxTaskBarIconEvent& WXUNUSED(event))
{   
    TrayIconMenu menu;// = new TrayIconMenu();
    //wxMenu menu;
    if (m_insertOpenSettingsMenu)
        menu.Append(ID_OPENSETTINGS,GETLANG(25));
    menu.Append(wxID_EXIT,GETLANG(26));   

    wxGetApp().m_MenuShown = &menu;
    PopupMenu((wxMenu *)&menu);        
}
